package com.liu.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liu.po.UserInfo;
import com.liu.service.UserInfoService;

@Controller
public class UserInfoController {
	@Resource(name="userInfoServiceImpl")
	private UserInfoService service;
	@RequestMapping("/addUser.do")
	public String addUser(UserInfo userInfo,Model model) {
		
		System.out.println(userInfo);
		boolean mark = service.addUser(userInfo);
		if (mark) {
			model.addAttribute("info", "用户信息添加成功");
			
		}else {
			model.addAttribute("info", "用户信息添加失败");
		}
		return "userinfo";
	}
	@RequestMapping("/queryUser.do")
	public String queryUser(UserInfo userInfo,Model model) {
		 
		System.out.println(userInfo);
		List<UserInfo> list = service.queryUserByName(userInfo);
		System.out.println(list);
		try {
				model.addAttribute("list", list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "userinfo_query";
	}
	
	@RequestMapping("/queryUserById.do")
	public String queryUserbyId(Integer userId,Model model) {
		 
		try {
			UserInfo user = service.queryUserById(userId);
				model.addAttribute("user", user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "userinfo_update";
	}
	
	@RequestMapping("/updateUser.do")
	public String updateyUser(UserInfo user,Model model) {
		 
		try {
			boolean b = service.updateUser(user);
			if (b) {
				model.addAttribute("updateInfo", "修改成功");
			}else{
				model.addAttribute("updateInfo", "修改失败");
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			model.addAttribute("updateInfo", "修改失败");
			e.printStackTrace();
		}
		return "userinfo_query";
	}
}
